import com.item.Application;
import com.item.service.ArticleService;
import com.item.service.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest(classes = {Application.class,testArticle.class})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class testArticle {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void before(){
        // 指定WebApplicationContext，将会从该上下文获取相应的控制器并得到相应的MockMvc
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        System.out.println("单元测试开始执行...");
    }


    @Test
    public void insertArticle() throws Exception {
        mockMvc.perform(
                put("http://localhost:9000/article")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式
                        .content("{\"title\":\"跑步很好\",\"content\":\"跑步6666\"}") // JSON 格式传参
        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }


    @Test
    public void updateArticle() throws Exception {
        mockMvc.perform(
                post("http://localhost:9000/article")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式
                        .content("{\"id\":\"8\",\"title\":\"跑步很好\",\"content\":\"跑步6666\"}")
        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }


    @Test
    public void selectAll() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void selectById() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article/selectById/1")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void selectByCollect() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article/collect")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void selectByType() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article/selectByTitle/篮球")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void selectHotList() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article/hotList")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void deleteArticle() throws Exception {
        mockMvc.perform(
                delete("http://localhost:9000/article/9")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }



    @Test
    public void delBatch() throws Exception {
        mockMvc.perform(
                post("http://localhost:9000/article/del/batch")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式
                        .content("[\"9\",\"10\"]")
        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @Test
    public void getPageListUser() throws Exception {
        mockMvc.perform(
                get("http://localhost:9000/article/pageList?page=1&pageSize=3&type=''&title=''")
                        .contentType(MediaType.APPLICATION_JSON_UTF8) // 传参格式

        )
                .andExpect(status().isOk()) // 添加断言
                .andDo(MockMvcResultHandlers.print()) // 打印操作
                .andReturn();
    }

    @After
    public void after(){
        System.out.println("单元测试执行结束...");
    }

	//已完成

}
